<?php

declare (strict_types=1);

namespace app\api\controller;



use app\common\exception\BaseException;
use app\api\service\User as UserService;
use app\api\validate\User as UserValidator;
use think\App;
use think\response\Json;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    private $userService;
    public function __construct(App $app,UserService $userService)
    {
        parent::__construct($app);
        
        $this->userService = $userService;
    }

    /**
     * 当前用户详情
     * @return Json
     * @throws BaseException
     */
    public function info(): Json
    {
        // 当前用户信息
        $userInfo = $this->userService->getCurrentLoginUser(true);
        // 获取会员等级
        $userInfo['grade'];
        $userInfo->invitation_code = encodeInviteCode($userInfo->user_id);
        return $this->renderSuccess(compact('userInfo'));
    }

    /**
     * 账户资产
     * @return Json
     * @throws BaseException
     */
    public function assets(): Json
    {
        $res = $this->userService->assets();
        return $this->renderSuccess($res);
    }

    /**
     * 手机号绑定
     * @return Json
     * @throws \cores\exception\BaseException
     */
    public function bindMobile(): Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'mobile|手机号' => 'require',
               // 'smsCode|验证码' => 'require',
                'type|类型' => 'require'  //1微信获取的手机号  2验证码绑定
            ]
        );
        if ($params['type'] == 2){
            if(!isset($params['smsCode'])){
                return $this->renderError('请输入验证码');
            }
        }
        $access_token = $this->request->header('access-token');
        if(!$access_token){
            return $this->renderError('参数错误');
        }
        $res = $this->userService->bindMobile($params,$access_token);
        if(!$res){
            return $this->renderError( '操作失败');
        }
        return $this->renderSuccess($res,'恭喜您，手机号绑定成功');
    }



    /**
     * 退出登录
     */
    public function loginOut():Json
    {
        $access_token = $this->request->header('access-token');
        if(!$access_token){
            return $this->renderError('参数错误');
        }
        $res = $this->userService->loginOut($access_token);
        if(!$res){
            return $this->renderError("退出异常");
        }
        return $this->renderSuccess([],'退出登录成功');
    }



    /**
     * 更新用户信息
     */
    public function update():Json
    {
        $params = $this->request->post(false);
        $this->validate(
            $params,
            [
               // 'avatar_url' => '',
                'nick_name' => 'require',
                'gender' => 'integer', // 1男  2女
                'mobile' => 'require',
            ]
        );

        $res = $this->userService->update($params);
        if(!$res){
            return $this->renderError('系统异常');
        }
        return $this->renderSuccess([],'用户信息更新成功');
    }

    /**
     * 小程序获取手机号信息
     */
    public function mobile():Json
    {
        $code = $this->request->param('code','');
        if(!$code){
            return $this->renderError('code参数为空');
        }
        $res = $this->userService->getMobile($code);
        return $this->renderSuccess($res);
    }

    /**补充邀请码
     * @return Json
     */
    public function supplementInvitationCode():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'invitation_code' => 'require',
            ]
        );
        $res = $this->userService->supplementInvitationCode();
        if(!$res){
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'补充成功');
    }

}
